package com.example.springboot.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.example.springboot.controller.requests.BaseRequst;
import com.example.springboot.controller.requests.UserPageRequst;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class UserService implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public List<User> list() {

        return userMapper.list();
    }
    @Override
    public Object page(BaseRequst userPageRequst)
    {

        PageHelper.startPage(userPageRequst.getPageNum(),
                userPageRequst.getPageSize());//前端传入
        List<User>  users=userMapper.listBycondition(userPageRequst);
       return new PageInfo<>(users);

    }

    @Override
    public void save(User user) {
        //当作卡号
        Date date=new Date();
        user.setUsername(DateUtil.format(date,"yyyyMMdd")+Math.abs(IdUtil.simpleUUID().hashCode()));
        userMapper.save(user);
    }

    @Override
    public User getByid(Integer id) {
return userMapper.getByid(id);
    }

    @Override
    public void update(User user) {
//        Date date=new Date();
//        user.setUsername(DateUtil.format(date,"yyyyMMdd")+IdUtil.simpleUUID());
          user.setUpdatetime(new Date());
        userMapper.updateById(user);

    }

    @Override
    public void delete(Integer id) {
        userMapper.deleteByid(id);
    }

}
